package newPan3d.content.ground.groundUp
{
	import _Pan3D.display3D.Display3DSprite;
	import _Pan3D.display3D.analysis.AnalysisServer;
	import _Pan3D.load.LoadInfo;
	import _Pan3D.load.LoadManager;
	import _Pan3D.program.Program3DManager;
	import _Pan3D.program.shaders.GroundScanningHightShader;
	import _Pan3D.texture.TextureManager;
	
	import _me.Scene_data;
	
	import flash.display.BitmapData;
	import flash.display3D.Context3D;
	import flash.display3D.Context3DProgramType;
	import flash.display3D.Context3DVertexBufferFormat;
	import flash.display3D.Program3D;
	import flash.geom.Matrix3D;
	import flash.geom.Point;
	import flash.geom.Vector3D;
	
	public class GroundUpDisplay3DSprite extends Display3DSprite
	{
		private var _groundPicBmp:BitmapData;//用于存放地型图片
		public function GroundUpDisplay3DSprite(context:Context3D)
		{
			super(context);
		}

		public function set groundPicBmp(value:BitmapData):void
		{
			_groundPicBmp = value;
		}

		public function get groundPicBmp():BitmapData
		{
			return _groundPicBmp;
		}

		private function get sunMatrix3D():Matrix3D
		{
			var sunMatrix3D:Matrix3D=new Matrix3D;
			sunMatrix3D.prepend(Scene_data.sun3D.cameraMatrix);
			sunMatrix3D.prepend(posMatrix);
			return sunMatrix3D
		}
		private function get lightMatrix3D():Matrix3D
		{
			var lightMatrix3D:Matrix3D=new Matrix3D;
			lightMatrix3D.prepend(Scene_data.light3D.cameraMatrix);
			lightMatrix3D.prepend(posMatrix);
			return lightMatrix3D
		}
		override public function updataAndScanning():void
		{
			if (!this._visible) {
				return;
			}
			if (_objData && _objData.texture) {
				//选择用扫描地形的着色器
				var tempPrograme3D:Program3D = Program3DManager.getInstance().getProgram(  GroundScanningHightShader.GROUND_SCANNING_HIGHT_SHADER);
				_context.setProgram(tempPrograme3D);
				setScanningVc();
				setScanningVa();
				resetScanningVa();
				
			}
		}
		override protected function setScanningVa() : void {
			_context.setVertexBufferAt(0, _objData.vertexBuffer, 0, Context3DVertexBufferFormat.FLOAT_3);
			_context.setVertexBufferAt(1, _objData.uvBuffer, 0, Context3DVertexBufferFormat.FLOAT_2);
			_context.setTextureAt(0, _objData.texture);
			_context.drawTriangles(_objData.indexBuffer, 0, -1);
		}
		
		override protected function resetScanningVa() : void {
			_context.setVertexBufferAt(0, null);
			_context.setVertexBufferAt(1, null);
			_context.setTextureAt(0,null);
		}
		override protected function setScanningVc() : void {
			this.updateMatrix();
			//modelMatrix.prependScale(2,0.2,2)
			_context.setProgramConstantsFromMatrix(Context3DProgramType.VERTEX, 4, modelMatrix, true);
			//传入一个缩放比值进来让显示对应像在平行线上1024也就是用来存取的大小，暂时统一;
			var _grounHightMapSize:int=Scene_data.grounHightMapSize;
			var _grounHightMapScale:int=Scene_data.grounHightMapScale;
			_context.setProgramConstantsFromVector(Context3DProgramType.VERTEX, 8, Vector.<Number>([_grounHightMapSize*_grounHightMapScale, 0, 0, 0]));
			_context.setProgramConstantsFromVector(Context3DProgramType.FRAGMENT,5, Vector.<Number>([256, 256, 1, 1]));
		}
		override protected function setVc() : void {
			this.updateMatrix();
			_context.setProgramConstantsFromMatrix(Context3DProgramType.VERTEX, 4, modelMatrix, true);
			_context.setProgramConstantsFromMatrix(Context3DProgramType.VERTEX, 8, sunMatrix3D, true);
			_context.setProgramConstantsFromMatrix(Context3DProgramType.VERTEX, 12, lightMatrix3D, true);
			
			
			
			
			//	var angle:Vector3D = new Vector3D(0.5,0.5,0.5,0);
			var angle:Vector3D = new Vector3D(Scene_data.sun3D.x,Scene_data.sun3D.y,Scene_data.sun3D.z,0);
			angle.normalize();
			_context.setProgramConstantsFromVector(Context3DProgramType.FRAGMENT,0,Vector.<Number>([angle.x,angle.y,angle.z,angle.w]));
			_context.setProgramConstantsFromVector(Context3DProgramType.FRAGMENT,1,Vector.<Number>( [Scene_data.radd,Scene_data.gadd,Scene_data.badd,0.5 ]));
			_context.setProgramConstantsFromVector(Context3DProgramType.FRAGMENT,2, Vector.<Number>([0, 0, 0.5, 1]));
			
			
		}
		override protected function setVa() : void {
			_context.setVertexBufferAt(0, _objData.vertexBuffer, 0, Context3DVertexBufferFormat.FLOAT_3);
			_context.setVertexBufferAt(1, _objData.uvBuffer, 0, Context3DVertexBufferFormat.FLOAT_2);
			_context.setVertexBufferAt(2,_objData.normalsBuffer, 0, Context3DVertexBufferFormat.FLOAT_3);
			_context.setTextureAt(0, Scene_data.groundLightText);
			_context.setTextureAt(1, _objData.texture);
			_context.setTextureAt(2,  Scene_data.texShadowMap);   //传入阴影体贴。用于对比是否背遮挡
			_context.drawTriangles(_objData.indexBuffer, 0, -1);
		}
		override protected function resetVa() : void {
			_context.setVertexBufferAt(0, null);
			_context.setVertexBufferAt(1, null);
			_context.setVertexBufferAt(2, null);
			_context.setTextureAt(0,null);
			_context.setTextureAt(1,null);
			_context.setTextureAt(2,null);
		}
		override public function set url(value : String) : void {
			_url = value;
			var loaderinfo : LoadInfo = new LoadInfo(Scene_data.fileRoot+"ground/obj/"+ _url + ".xml", LoadInfo.XML, onObjLoad, false);
			LoadManager.getInstance().addSingleLoad(loaderinfo);
		}
		
		override protected function onObjLoad(str : String) : void {
			_objData = AnalysisServer.getInstance().analysisObj(str);
			TextureManager.getInstance().addTexture(Scene_data.fileRoot+"ground/texture/" + _objData.mtl + ".jpg", addTexture, null);
		}
		
		override protected function uplodToGpu():void
		{
			var i:int,j:int,k:int;
			var xd:Number=10;
			var zd:Number=10;
			var xmove:Number=0;
			var zmove:Number=0;
			var vv:Vector.<Number>=new Vector.<Number>;
			var uu:Vector.<Number>=new Vector.<Number>;
			var ss:Vector.<Number>=new Vector.<Number>; //要确定每组成几个数据;暂为3跟顶点一样
			var ii:Vector.<uint>=new Vector.<uint>;
			
		    var y0:int=0;
		    var y1:int=0;
		    var y2:int=0;
		    var y3:int=0;
		
			var rectNum:int=0;
			for(i=0;i<100;i++){
				for(j=0;j<100;j++){
					xmove=i*xd
					zmove=j*zd;
					var picType:int=getGroundPicAre(i,j);
				
					
					//	if(y0+y1+y2+y3){
					if(picType!=0){
						vv.push(0+xmove,0,0+zmove);
						vv.push(0+xmove,0,zd+zmove);
						vv.push(xd+xmove,0,0+zmove);
						vv.push(xd+xmove,0,zd+zmove);
						var P:Point=new Point(0,0);
					
						switch(picType)
						{
							case 5:
								uu.push(P.x+0.333,P.y+0.333,P.x+0.333,P.y+0.666,P.x+0.666,P.y+0.333,P.x+0.666,P.y+0.666);
								break;
							case 1:
								uu.push(P.x+0.000,P.y+0.000,P.x+0.000,P.y+0.333,P.x+0.333,P.y+0.000,P.x+0.333,P.y+0.333);
								break;
							case 2:
								uu.push(P.x+0.333,P.y+0.000,P.x+0.333,P.y+0.333,P.x+0.666,P.y+0.000,P.x+0.666,P.y+0.333);
								break;
							case 3:
								uu.push(P.x+0.000,P.y+0.333,P.x+0.333,P.y+0.333,P.x+0.000,P.y+0.000,P.x+0.333,P.y+0.000);
								break;
							case 4:
								uu.push(P.x+0.000,P.y+0.333,P.x+0.000,P.y+0.666,P.x+0.333,P.y+0.333,P.x+0.333,P.y+0.666);
								break;
							case 6:
								uu.push(P.x+0.333,P.y+0.333,P.x+0.333,P.y+0.666,P.x+0.000,P.y+0.333,P.x+0.000,P.y+0.666);
								break;
							case 7:
								uu.push(P.x+0.000,P.y+0.333,P.x+0.000,P.y+0.000,P.x+0.333,P.y+0.333,P.x+0.333,P.y+0.000);
								break;
							case 8:
								uu.push(P.x+0.333,P.y+0.333,P.x+0.333,P.y+0.000,P.x+0.666,P.y+0.333,P.x+0.666,P.y+0.000);
								break;
							case 9:
								uu.push(P.x+0.333,P.y+0.333,P.x+0.333,P.y+0.000,P.x+0.000,P.y+0.333,P.x+0.000,P.y+0.000);
								break;
							case 11:
								uu.push(P.x+0.999,P.y+0.001,P.x+0.999,P.y+0.333,P.x+0.677,P.y+0.001,P.x+0.677,P.y+0.333);
								break;
							case 12:
								uu.push(P.x+0.677,P.y+0.001,P.x+0.677,P.y+0.333,P.x+0.999,P.y+0.000,P.x+0.999,P.y+0.333);
								break;
							case 13:
								uu.push(P.x+0.999,P.y+0.333,P.x+0.999,P.y+0.0,P.x+0.677,P.y+0.333,P.x+0.677,P.y+0.0);
								break;
							case 14:
								uu.push(P.x+0.677,P.y+0.333,P.x+0.677,P.y+0.0,P.x+0.999,P.y+0.333,P.x+0.999,P.y+0.0);
								break;
	
							default:
								uu.push(P.x+0.333,P.y+0.333,P.x+0.333,P.y+0.666,P.x+0.666,P.y+0.333,P.x+0.666,P.y+0.666);
								break;
						}

						ss.push(0,1,0,0,1,0,0,1,0,0,1,0);
						
						ii.push(0+rectNum*4,1+rectNum*4,2+rectNum*4);
						ii.push(1+rectNum*4,3+rectNum*4,2+rectNum*4);
						
						rectNum++;
					}
				}
			}
			/*
			0,0,0,0,0,1000,1000,0,0,1000,0,1000,1000,0,0,0,0,1000
			0,0,0,1,1,0,1,1,1,0,0,1
			0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0
			0,1,2,3,4,5
			*/
			
			trace(_objData.vertices);
			trace(_objData.uvs);
			trace(_objData.normals);
			trace(_objData.indexs);
			
			
			
			
			 _objData.vertices=vv;
			 _objData.uvs=uu;
			 _objData.normals=ss;
			 _objData.indexs=ii;
			 
			_objData.vertexBuffer = this._context.createVertexBuffer(_objData.vertices.length / 3, 3);
			_objData.vertexBuffer.uploadFromVector(Vector.<Number>(_objData.vertices), 0, _objData.vertices.length / 3);
			
			_objData.uvBuffer = this._context.createVertexBuffer(_objData.uvs.length / 2, 2);
			_objData.uvBuffer.uploadFromVector(Vector.<Number>(_objData.uvs), 0, _objData.uvs.length / 2);
			
			_objData.normalsBuffer = this._context.createVertexBuffer(_objData.normals.length / 3, 3);
			_objData.normalsBuffer.uploadFromVector(Vector.<Number>(_objData.normals), 0, _objData.normals.length / 3);
			
			_objData.indexBuffer = this._context.createIndexBuffer(_objData.indexs.length);
			_objData.indexBuffer.uploadFromVector(Vector.<uint>(_objData.indexs), 0, _objData.indexs.length);
			
		}
		private function getGroundPicHigh(i:int,j:int):Number
		{
			var a:Number=_groundPicBmp.getPixel(i,j)
			var b:Number=_groundPicBmp.getPixel(i-1,j-1)
			var c:Number=_groundPicBmp.getPixel(i-1,j)
			var d:Number=_groundPicBmp.getPixel(i,j-1)
			
			return (a+b+c+d)/40;
		}
		private function getBitmapIsHit(i:int,j:int):int
		{
			var a:Number=_groundPicBmp.getPixel(i,j)
			a=a>0xaa0000?1:0	
			return a;
		}
		private function getGroundPicAre(i:int,j:int):Number
		{
			var a:Number=_groundPicBmp.getPixel(i-1,j)
			var b:Number=_groundPicBmp.getPixel(i+1,j)
			var c:Number=_groundPicBmp.getPixel(i,j-1)
			var d:Number=_groundPicBmp.getPixel(i,j+1)
			var hit:Number=_groundPicBmp.getPixel(i,j)
				
			a=a>0xaa0000?1:0	
			b=b>0xaa0000?1:0	
			d=d>0xaa0000?1:0	
			c=c>0xaa0000?1:0	
			hit=hit>0xaa0000?1:0	
		    if(hit){
				if(a==0&&b==1&&c==0&&d==1){
					return 1;
				}
				if(a==1&&b==1&&c==0&&d==1){
					return 2;
				}
				if(a==1&&b==0&&c==0&&d==1){
					return 3;
				}
				if(a==0&&b==1&&c==1&&d==1){
					return 4;
				}
				if(a==1&&b==1&&c==1&&d==1){
					
					//处理边角
					if(!getBitmapIsHit(i-1,j-1)){
						return 11
					}
					if(!getBitmapIsHit(i+1,j-1)){
						return 12
					}
					if(!getBitmapIsHit(i-1,j+1)){
						return 13
					}
					if(!getBitmapIsHit(i+1,j+1)){
						return 14
					}
					
					
					return 5;
				}
				if(a==1&&b==0&&c==1&&d==1){
					return 6;
				}
				if(a==0&&b==1&&c==1&&d==0){
					return 7;
				}
				if(a==1&&b==1&&c==1&&d==0){
					return 8;
				}
				if(a==1&&b==0&&c==1&&d==0){
					return 9;
				}
				

				
			}
			return 0;
		}
		
		
	}
}